{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is the RedELK Welcome Jupyter Notebook\n",
    "# We will go through a few steps to make sure the connections to Elasticsearch and BloodHound/Neo4J are operational\n",
    "\n",
    "import urllib3\n",
    "import ssl\n",
    "from elasticsearch import Elasticsearch\n",
    "from elasticsearch.connection import create_ssl_context\n",
    "from redelk_passwords import * # this file contains all relevant credentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define the connection to our ES instance\n",
    "\n",
    "urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n",
    "ssl_context = create_ssl_context()\n",
    "ssl_context.check_hostname = False\n",
    "ssl_context.verify_mode = ssl.CERT_NONE\n",
    "\n",
    "es = Elasticsearch(\n",
    "    ['redelk-elasticsearch'],\n",
    "    http_auth=(CredESUsername, CredESPassword),\n",
    "    scheme=\"https\",\n",
    "    port=9200,\n",
    "    ssl_context=ssl_context\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now test the connection to ES -- this should give output\n",
    "\n",
    "if es.ping():\n",
    "    print(\"ES connection successful\")\n",
    "else:\n",
    "    raise ValueError(\"ES Connection failed\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now check the connection to Neo4j -- this should give output\n",
    "\n",
    "from neo4j import GraphDatabase\n",
    "\n",
    "class HelloWorldExample:\n",
    "\n",
    "    def __init__(self, uri, user, password):\n",
    "        self.driver = GraphDatabase.driver(uri, auth=(user, password))\n",
    "\n",
    "    def close(self):\n",
    "        self.driver.close()\n",
    "\n",
    "    def print_greeting(self, message):\n",
    "        with self.driver.session() as session:\n",
    "            greeting = session.write_transaction(self._create_and_return_greeting, message)\n",
    "            print(greeting)\n",
    "\n",
    "    @staticmethod\n",
    "    def _create_and_return_greeting(tx, message):\n",
    "        result = tx.run(\"CREATE (a:Greeting) \"\n",
    "                        \"SET a.message = $message \"\n",
    "                        \"RETURN a.message + ', from node ' + id(a)\", message=message)\n",
    "        return result.single()[0]\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    greeter = HelloWorldExample(\"bolt://redelk-bloodhound-neo4j:7687\", CredNeo4jUsername, CredNeo4jPassword)\n",
    "    greeter.print_greeting(\"hello, world\")\n",
    "    greeter.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
